package me.leoray.leilog;

import android.util.Log;

/**
 * 抽象的PipeLine,包含了两个通用配置:代理和输出级别
 * Created by leoraylei on 16/8/28.
 */
public abstract class AbstractPipeLine implements PipeLine {
    protected LogProxy logProxy; //设置代理日志类
    protected int level = Log.DEBUG; //最低日志输出级别

    /**
     * 设置输出级别
     *
     * @param level
     * @return
     */
    public AbstractPipeLine level(int level) {
        this.level = level;
        return this;
    }

    /**
     * 设置代理
     *
     * @param proxy
     */
    public AbstractPipeLine setLogProxy(LogProxy proxy) {
        this.logProxy = proxy;
        return this;
    }

    @Override
    public void pipeline(int logType, String tag, String message, Throwable t) {
        if (logType < level) {
            return;
        }
        if (logProxy != null) {
            switch (logType) {
                case Log.VERBOSE:
                    logProxy.v(tag, message);
                    break;
                case Log.DEBUG:
                    logProxy.d(tag, message);
                    break;
                case Log.INFO:
                    logProxy.i(tag, message);
                    break;
                case Log.WARN:
                    logProxy.w(tag, message);
                    break;
                case Log.ERROR:
                    logProxy.e(tag, message);
                    break;
                case Log.ASSERT:
                    logProxy.wtf(tag, message);
                    break;

            }
        } else {
            log(logType, tag, message, t);
        }
    }

    protected abstract void log(int logType, String tag, String message, Throwable t);
}
